/** 自定义字符串工具*/
export default class L_String{
 
public static getNumberStrWith2Len(v:number):string | null{
    if(v<10){
        return "0"+v
    }else if(v>=10 && v<100){
        return ""+v
    }else{
        console.log("getNumberStrWith2Len()错误调用! 参数不能>99")
        return null
    }

}
 
//获取对象属性格式化后的类JSON字符串 
public static getObjJSONStr(obj:any):string{
    if(obj==null || obj==undefined) return "null obj"
    return this.getObjJSONStrFmt(obj,true)
} 

private  static getObjJSONStrFmt(obj:any,shellSymbol:boolean):string{
    let rs:string="";
    let shellLeft="{";  //最后的外壳
    let shellRight="}";
    if(Array.isArray(obj)){
        rs=this.getArrJSONStrFmt(obj);
        return rs;
    }
    if(obj instanceof Set){
        shellLeft="["
        shellRight="]"
        for (let entry of obj) {
            let trs=this.trsfToStr(null,entry)
            if(trs!=null) rs=rs+","+trs
        }
    }else if(obj instanceof Map){
        for (let [key, value] of obj) {
            let trs=this.trsfToStr(key,value)
            if(trs!=null) rs=rs+","+trs+"\n"
        }
    }else{
        // for(let k in obj){
        //     let v=obj[k]
        //     if(typeof v=="function"){
        //         continue;
        //     }else if(Array.isArray(v)){
        //         rs=rs+","+k+":"+this.getArrJSONStrFmt(v);
        //         // rs=rs+","+k+":"+"["
        //         // for(let ak=0;ak<v.length;ak++){
        //         //     let av=v[ak]
        //         //     if(typeof av== "object"){
        //         //         rs=rs+this.getObjJSONStrFmt(av,true)
        //         //     }else{
        //         //         rs=rs+av
        //         //     }
        //         //     if(ak!=v.length-1){
        //         //         rs=rs+","
        //         //     }
        //         // }
        //         // rs=rs+"]"      
        //     }else if(typeof v== "object"){
        //         rs=rs+","+k+":"+this.getObjJSONStrFmt(v,true)
        //     }else{
        //         if(v==undefined || v==null){
        //             continue;
        //         }
        //         rs=rs+","+k+":"+v
        //     }
        // }
        for(let k in obj){
            let trs=this.trsfToStr(k,obj[k])
            if(trs!=null) rs=rs+","+trs
        }
    }
    return shellSymbol?shellLeft+rs.substring(1)+shellRight:rs.substring(1)
}

private  static trsfToStr(key:any,value:any):string | null{
    let ist:string=typeof value
    if(ist=="function"){
        return null
    }else if(Array.isArray(value)){
        return key+":"+this.getArrJSONStrFmt(value)
    }else if(value instanceof Set){
        return "["+this.getObjJSONStrFmt(value,true)+"]"
    }else if(ist=="object"){
        return key+":"+this.getObjJSONStrFmt(value,true)
    }else{
        if(value==undefined || value==null){
            return null
        }
        return key!=null?(key+":"+value):value
    }
}

private  static getArrJSONStrFmt(arr:any[]):string{
    let rs:string="[";
    for(let ak=0;ak<arr.length;ak++){
        let av=arr[ak]
        if(typeof av== "object" ){
            rs=rs+this.getObjJSONStrFmt(av,true)
        }else{
            rs=rs+av
        }
        if(ak!=arr.length-1){
            rs=rs+","
        }
    }
    return rs+"]";
}
 

  
}  